package org.example.diet_compass.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.diet_compass.domain.Comments;
import org.example.diet_compass.dto.CommentsListDto;
import org.example.diet_compass.service.CommentsService;
import org.example.diet_compass.mapper.CommentsMapper;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 86135
 * @description 针对表【comments(评论表)】的数据库操作Service实现
 * @createDate 2025-03-02 17:14:01
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments>
        implements CommentsService {
    @Resource
    CommentsMapper commentsMapper;

    @Override
    public void updateLikeNums(Long commentId,int Num) {
        commentsMapper.updateLikeNums(commentId,Num);
    }

    @Override
    public void removeByCommentId(Integer commentId) {
        commentsMapper.removeByCommentId(commentId);
    }

    @Override
    public Integer getGoodCommentTotal(String goodId) {
        return commentsMapper.getGoodCommentTotal(goodId);
    }

    @Override
    public List<CommentsListDto> getGoodsComments(String goodsId) {

        //将多表联查的结果封装到CommentsListDto对象中
        List<Map<String, Object>> goodsComments = commentsMapper.getGoodsComments(goodsId);
        ArrayList<CommentsListDto> commentsListDtoArrayList = new ArrayList<>(goodsComments.size());

        //循环遍历封装后加到数组中
        for (Map<String, Object> row : goodsComments) {
            CommentsListDto commentsListDto = new CommentsListDto();
            commentsListDto.setId((Long) row.get("comment_id"));
            commentsListDto.setParent_id((Long) row.get("parent_id"));
            commentsListDto.setReply_id((Long) row.get("reply_id"));
            commentsListDto.setReply_name((String) row.get("reply_name"));
            commentsListDto.setUser_id((Integer) row.get("user_id"));
            commentsListDto.setUser_name((String) row.get("user_name"));
            commentsListDto.setUser_avatar((String) row.get("user_avatar"));
            commentsListDto.setUser_content((String) row.get("user_content"));
            commentsListDto.setIs_like(((Integer) row.get("is_like")) == 1);
            commentsListDto.setLike_count((Integer) row.get("like_count"));
            LocalDateTime createdAt = (LocalDateTime) row.get("created_at");
            String format = createdAt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            commentsListDto.setCreate_time(format);
            commentsListDtoArrayList.add(commentsListDto);
        }


        return commentsListDtoArrayList ;
    }

    @Override
    public List<Comments> getCommentsByUserId(int userId) {
        return commentsMapper.getCommentsByUserId(userId);
    }

    @Override
    public void likeComment(long commentId) {
        commentsMapper.likeComment(commentId);
    }
}




